package com.baoyi.pooke;

import net.youmi.android.AdManager;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.baoyi.adapter.MainDataAdapter;
import com.baoyi.content.content;
import com.baoyi.widget.ListItemWidget;

/**
 * 拍客主界面，显示网络中拍的照片，已上传时间排序
 * 
 * @author Administrator
 * 
 */
public class MainUI extends Activity {
	static {
		AdManager.init(content.id, content.pwd, content.time, content.con,
				content.banben);
	}
	public static DisplayMetrics displayMetrics;
	MainDataAdapter adapter;
	ProgressBar progressBar;
	private ImageButton button2;
	private ImageButton button3;
	private ListView imglist;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imglist = (ListView) findViewById(R.id.imglist);
		button2 = (ImageButton) findViewById(R.id.button2);
		button2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Intent item = new Intent(MainUI.this, LocGalleryItem.class);
				Intent item = new Intent(MainUI.this, PaiKuUI.class);
				startActivity(item);

			}
		});
		button3 = (ImageButton) findViewById(R.id.button3);
		button3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent item = new Intent(MainUI.this, CamActivity.class);
				startActivity(item);

			}
		});
		displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

		imglist.setDividerHeight(0);

	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		new DownloadTask().execute("");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (BaoyiApplication.getInstance().isonline()) {
			new DownloadTask().execute("");
			imglist.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					ListItemWidget imageView = (ListItemWidget) arg1;
					Intent item = new Intent(MainUI.this, ImageViewUI.class);
					item.putExtra("id", imageView.getImageItem().getId());
					startActivity(item);
				}
			});
			imglist.setOnScrollListener(new OnScrollListener() {

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem,
						int visibleItemCount, int totalItemCount) {
					Log.d(BaoyiApplication.TAG, "firstVisibleItem:"
							+ firstVisibleItem + "visibleItemCount"
							+ visibleItemCount + ">>" + totalItemCount);

					if (firstVisibleItem > totalItemCount
							- (visibleItemCount + 1)) {
						if (null != adapter) {
							Log.d(BaoyiApplication.TAG, "添加东西");
							if (adapter.add()) {
								imglist.setAdapter(adapter);
								imglist.setSelection(firstVisibleItem);
								adapter.notifyDataSetChanged();
							}
						}
					}
				}

				@Override
				public void onScrollStateChanged(AbsListView view,
						int scrollState) {
					// TODO Auto-generated method stub
					Log.d(BaoyiApplication.TAG, scrollState + ">>");
				}
			});
		} else {
			Toast.makeText(MainUI.this, "请检查你的网络连接！",
					Toast.LENGTH_SHORT).show();

		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 2, 0, "本地拍客");
		menu.add(0, 1, 0, "关于我们");
		// menu.add(0, 3, 0, "设置");
		return true;
	}

	/**
	 * 菜单的方式设置壁纸
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// 设置壁纸
		if (item.getItemId() == 1) {
			Intent intent = new Intent(MainUI.this, AboutUI.class);
			startActivity(intent);
		}
		// 显示顶以后的消息
		if (item.getItemId() == 2) {
			Intent intent = new Intent(MainUI.this, LocGalleryItem.class);
			startActivity(intent);
		}
		// 返回到首页
		// if (item.getItemId() == 3) {
		// Intent intent = new Intent(MainUI.this, AboutUI.class);
		// startActivity(intent);
		// }
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 下载图片
	 * 
	 * @author Lukasz Wisniewski
	 */
	class DownloadTask extends AsyncTask<String, Void, String> {

		private String mTaskUrl;

		@Override
		public void onPreExecute() {

			super.onPreExecute();
			Toast.makeText(MainUI.this, "系统正在加载内容，请等待...", Toast.LENGTH_SHORT)
					.show();
		}

		@Override
		public String doInBackground(String... params) {
			adapter = new MainDataAdapter(MainUI.this);
			return mTaskUrl;
		}

		@Override
		public void onPostExecute(String url) {
			super.onPostExecute(url);
			imglist.setAdapter(adapter);
		}

	};

}